<?php

use think\Db;

/**
 * @param int $time  时间戳
 *
 * @return string
 */
function func_friendly_time($time = 0)
{
    if (empty($time) || $time < 0) {
        return '';
    }

    $diff = time() - $time;

    if ($diff <= 0 || $diff < 60) {
        return '刚刚';
    }

    $i = floor($diff / 60); //分
    $h = floor($diff / 60 / 60); //时
    $d = floor($diff / 86400); //日
    $m = floor($diff / 2592000); //月
    $y = floor($diff / 60 / 60 / 24 / 365); //年

    if ($i < 30) {
        return $i . '分钟前';
    }

    if ($i > 30 && $i < 60) {
        return '一小时内';
    }

    if ($h >= 1 && $h < 24) {
        return $h . '小时前';
    }

    if ($d >= 1 && $d < 30) {
        return $d . '天前';
    }

    if ($m >= 1 && $m < 12) {
        return $m . '个月前';
    }

    if ($y) {
        return $y . '年前';
    }

    return "";
}

/**
 * @param count 获取图片个数
 * @param content 要匹配的内容
 * @return Array
 */
function func_get_img($content = '', $count = 0)
{
    $regImg = '/<img[^>]*src\s*=\s*[\"|\']?\s*([^>\"\'\s]*)(\">|\"\/>)/i';
    $res    = preg_match_all($regImg, $content);
    return $res;
}

/**
 * 发送delete请求
 * @param string $url 请求地址
 * @param array $header 请求头
 * @param array $postdata post键值对数据
 * @return string
 */
function send_delete($url, $header, $postdata = [])
{
    $curl    = curl_init();
    $headers = empty($header) ? array(
        'method'  => 'POST',
        'header'  => 'Content-type:application/json;charset=utf-8',
        // 'content' => $postdata,
        'timeout' => 15 * 60, // 超时时间（单位:s）
    ) : $header;
    if (count($headers) >= 1) {
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    }
    curl_setopt($curl, CURLOPT_URL, $url);

    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);

    if (!empty($postdata)) {
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
    }

    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    $output   = curl_exec($curl);
    $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);

    curl_close($curl);

    $data['code'] = $httpCode;
    $data['data'] = json_decode($output, true);
    return $data;
}
